#include "Stateman.h"



Stateman::Stateman(int initialsize)
{
	m_currentstate=NULL;
	m_numstates=initialsize;
	m_states=new State*[m_numstates];
	for (int i=0;i<m_numstates;i++)
	{
		m_states[i]=NULL;
	}
}

Stateman::~Stateman()
{
	for (int i=0;i<m_numstates;i++)
	{
		if (m_states[i]!=NULL)
		{
			delete m_states[i];
		}
	}
	delete []m_states;
}

void Stateman::Addstate(State *state)
{
	for (int i=0;i<m_numstates;i++)
	{
		if (m_states[i]==NULL)
		{
			m_states[i]=state;
			break;
		}
	}
}

bool Stateman::Removestate(State *state)
{
	for (int i=0;i<m_numstates;i++)
	{
		if (m_states[i]==state)
		{
			delete m_states[i];
			m_states[i]=NULL;
			return true;
		}
	}
	return false;
}

bool Stateman::Removestate(int ID)
{
	for (int i=0;i<m_numstates;i++)
	{
		if (m_states[i]!=NULL)
		{
			if (m_states[i]->getID()==ID)
			{
				delete m_states[i];
				m_states[i]=NULL;
				return true;
			}
		}
	}
	return false;
}

State *Stateman::getState(int ID)
{
	for (int i=0;i<m_numstates;i++)
	{
		if (m_states[i]!=NULL)
		{
			if (m_states[i]->getID()==ID)
			{
				return m_states[i];
			}
		}
	}
	return NULL;
}
	
State *Stateman::getCurrentState()
{
	return m_currentstate;
}

bool Stateman::EnterState(int ID)
{
	
	for (int i=0;i<m_numstates;i++)
	{

		if (m_states[i]!=NULL)
		{
			if (m_states[i]->getID()==ID)
			{
				if (m_currentstate!=NULL)
				{
					m_currentstate->Foldup();
				}
				m_currentstate=m_states[i];
				m_currentstate->Foldout();
				return true;
			}
		}
	}
	return false;
}